/**
 * Filename: MusicThemeAdapter.java
 * @author  Firefly
 * @contact postfirefly@163.com
 * @time    2016/5/8 16:23
 */
package com.github.postsanf.yinian.adapter;

import android.content.Context;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.bean.YNMusicTheme;
import com.github.postsanf.yinian.utils.DisplayUtils;
import com.github.postsanf.yinian.utils.ImageOptHelper;
import com.github.postsanf.yinian.utils.StringUtils;

import org.w3c.dom.Text;

import java.util.List;

public class MusicThemeAdapter extends RecyclerView.Adapter<MusicThemeAdapter.ViewHolder>
{
	private Context				context;
	private LayoutInflater 		mInflater;
	private List<YNMusicTheme> 	mDatas;


	public interface OnItemClickLitener
	{
		void onItemClick(View view, String  url,String proportion,int curid);
	}

	public MusicThemeAdapter(Context context, List<YNMusicTheme> datats)
	{
		this.context = context;
		mInflater = LayoutInflater.from(context);
		mDatas = datats;
	}

	private OnItemClickLitener mOnItemClickLitener;

	public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener)
	{
		this.mOnItemClickLitener = mOnItemClickLitener;
	}

	public static class ViewHolder extends RecyclerView.ViewHolder
	{
		public ViewHolder(View arg0)
		{
			super(arg0);
		}
		SimpleDraweeView  	mImg;
		TextView			mName;
	}

	@Override
	public int getItemCount()
	{
		return mDatas.size();
	}

	/**
	 * 创建ViewHolder
	 */
	@Override
	public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i)
	{
		View view = mInflater.inflate(R.layout.yn_item_music_theme,viewGroup, false);
		ViewHolder viewHolder = new ViewHolder(view);
		viewHolder.mImg = (SimpleDraweeView) view.findViewById(R.id.id_music_theme_item_image);
		viewHolder.mName = (TextView) view.findViewById(R.id.tv_music_theme_name);
		return viewHolder;
	}

	/**
	 * 设置值
	 */
	@Override
	public void onBindViewHolder(final ViewHolder viewHolder, final int i)
	{
		Uri uri = Uri.parse(mDatas.get(i).getTempletPic()+StringUtils.getQulity());
		ImageOptHelper.showFrescoImg(uri, viewHolder.mImg, context);
//		viewHolder.mName.setText(mDatas.get(i).getTempletName());
		if (mOnItemClickLitener != null)
		{
			viewHolder.itemView.setOnClickListener(new View.OnClickListener()
			{
				@Override
				public void onClick(View v)
				{
					mOnItemClickLitener.onItemClick(viewHolder.itemView, mDatas.get(i).getTempletUrl(),mDatas.get(i).getTempletProportion(),mDatas.get(i).getTempletID());
				}
			});
		}
		viewHolder.setIsRecyclable(false);
	}

}
